const router = require('koa-router')()
const { query } = require("../db")
const { URL } = require("url");
const path = require("path");
const fs = require("fs");
router.prefix('/collect');


//用户点击收藏时,发送post请求,把用户信息(user_id)和收藏内容(occult_id)存入数据库的collect表中
router.post('/',async ctx=> {
  let user_id = ctx.request.body.user_id
  let occult_id = ctx.request.body.occult_id
  let img_url = ctx.request.body.img_url
  let title = ctx.request.body.title
  let sql = "insert into collect (user_id, occult_id,img_url,title) values('" + user_id + "', '" + occult_id + "','"+img_url+"','"+title+"')";
  await query(sql, [])        
})

//取消收藏
router.post('/delete',async ctx=>{
    let user_id = ctx.request.body.user_id
    let occult_id = ctx.request.body.occult_id
    let sql = "delete from collect where user_id='"+ user_id + "'and occult_id='" + occult_id+"'"
    await query(sql, [])

})

//获取collect表中的数据
router.post('/person_collect',async ctx=>{
  let user_id = ctx.request.body.user_id
  let sql = "select * from collect where user_id='"+user_id+"'";
  let results = await query(sql, []);
  ctx.res.end(JSON.stringify(results));
})

module.exports = router